"保存vimrc后自动重载
"autocmd BufWritePost $MYVIMRC source $MYVIMRC

"================= 快捷键 ======================
"===============================================

	let mapleader=" "

  map <LEADER>rl :source $MYVIMRC<CR>

  "creat empty line
  noremap <LEADER>[ O<ESC>Dl
  noremap <LEADER>] o<ESC>Dk


	"调整分屏部分大小
	map <M-=> :vertical res -5<CR>
	map <M--> :vertical res +5<CR>
	map <M-+> :resize -5<CR>
	map <M-_> :resize +5<CR>

	map <A->= :vertical res -5<CR>
	map <A--> :vertical res +5<CR>
	map <A-+> :resize -5<CR>
	map <A-_> :resize +5<CR>

	"标签页
	map <C-h> :-tabnext<CR>
	map <C-l> :+tabnext<CR>

	"直接放到中间
	noremap <C-u> <C-u>zz
	noremap <C-d> <C-d>zz
	noremap <C-f> <C-f>zz
	noremap <C-b> <C-b>zz

  inoremap <M-l> <RIGHT>
  inoremap <M-h> <LEFT>
  inoremap <M-j> <DOWN>
  inoremap <M-k> <UP>
  inoremap <CR> <ESC>o

  cnoremap <C-p> <Up>
  cnoremap <C-n> <Down>

  " inoremap <C-l> <RIGHT>
  " inoremap <C-h> <LEFT>
  " inoremap <C-j> <DOWN>
  " inoremap <C-k> <UP>
	"删除一对括号
	"nnoremap <LEADER>( ma%x`ax
	"nnoremap <LEADER>) ma%x`ax

	"enable rainbow
	"set to 0 if you want to enable it later via :RainbowToggle
	let g:rainbow_active = 1

  "Compile function
  "noremap <LEADER>dc :!g++ -std=c++11 -g % -o %< <CR>
  noremap <LEADER>dc :!clang++ -std=c++11 -glldb % -o %<
  noremap <LEADER>dr :call CompileRunGcc()<CR>

  func! CompileRunGcc()
  	exec "w"
  	if &filetype == 'cpp'
  		"exec "!g++ -g % -o %<"
  		exec "!clang++ -std=c++11 -glldb % -o %<"
  		exec "!time ./%<"
    endif
  endfunc


"=============== 编辑器设置 ====================
"===============================================

  set nocompatible
  
  filetype on
  filetype indent on
  filetype plugin on
  filetype plugin indent on
  
  syntax on
  set number
  set relativenumber
  set cursorline
  set wrap
  set wildmenu
  "hight light search
  set hlsearch
  "instance search
  set incsearch
  exec "nohlsearch"
  set showmatch
  set wildmenu
  set smartcase
  "
  "激活鼠标的使用"
  set mouse="nv"
  set selection=exclusive
  set selectmode=mouse,key
  
  set list
  "set listchars=tab:↝-,space:⋅,trail:☢
  set listchars=tab:-ﴪ,trail:
  
  "使用不同的光标对应不同的模式
  let &t_SI="\<Esc>]50;CursorShape=1x7"
  let &t_SR="\<Esc>]50;CursorShape=2x7"
  
  "auto go to current dir
  set autochdir

  "expand history storage
  set history=200
  
  "For Tab
  set expandtab
  set tabstop=2
  set shiftwidth=2
  set softtabstop=2
  
  "自动返回上次编辑位置
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

  "au BufReadPost * if foldenable | 
  
  "代码折叠
  set nofoldenable
  "设置按照语法方式折叠（可简写set fdm=XX）"
  "有6种折叠方法：
  "manual   手工定义折叠"
  "indent   更多的缩进表示更高级别的折叠"
  "expr     用表达式来定义折叠"
  "syntax   用语法高亮来定义折叠"
  "diff     对没有更改的文本进行折叠"
  "marker   对文中的标志进行折叠"

  autocmd BufRead *.vim set foldmethod=indent
  autocmd BufRead *.py set foldmethod=indent
  "autocmd BufRead *.cpp set foldmethod=indent
  autocmd VimEnter * unmap <LEADER>tm
  autocmd VimEnter * unmap <LEADER>tt
  set foldmethod=syntax
  "设置为自动关闭折叠"
  "set foldclose=all
  
  set scrolloff=3
  
  "保留编辑历史
  silent !mkdir -p ~/.config/nvim/tmp/backup
  silent !mkdir -p ~/.config/nvim/tmp/undo
  set backupdir=~/.config/nvim/tmp/backup,.
  set directory=~/.config/nvim/tmp/backup,.
  if has('persistent_undo')
  	set undofile
  	set undodir=~/.config/nvim/tmp/undo
  endif


"================= 插件 ========================
"===============================================

call plug#begin('~/.local/share/nvim/plugged')

	"========== Bundles ===========
	"==============================

		"tagbar
    "Plug 'preservim/tagbar'
    "Replacing tag bar
    Plug 'liuchengxu/vista.vim'

	"defx
	"Plug 'Shougo/defx.nvim', { 'do': ':UpdateRemotePlugins' }
	"Plug 'kristijanhusak/defx-icons'

    "ranger
    Plug 'kevinhwang91/rnvimr'

		" Undo Tree
		Plug 'mbbill/undotree'

		"vim start up screen
		Plug 'mhinz/vim-startify'

		"coc
		Plug 'neoclide/coc.nvim', {'branch': 'release'}

		"snippets
    "Plug 'SirVer/ultisnips'
		Plug 'honza/vim-snippets'

    "emojy
    "Plug 'junegunn/vim-emoji'

    "gdb
    Plug 'sakhnik/nvim-gdb', { 'do': ':!./install.sh' }

    "fzf
    Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }

    "which-key
    Plug 'liuchengxu/vim-which-key'

    "All kinds of language support
    Plug 'sheerun/vim-polyglot'

    "Mark down table plugin
    Plug 'dhruvasagar/vim-table-mode'
    Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && yarn install' ,'for': ['markdown', 'vim-plug']}
    "Mark down generate content table
    Plug 'mzlogin/vim-markdown-toc'

    "cxxd for project management
    "Plug 'JBakamovic/cxxd-vim'
	"========== Interface enhance ===========
	"========================================
	
		"airline 底下的状态栏
		Plug 'vim-airline/vim-airline'
		Plug 'vim-airline/vim-airline-themes'

		"ranbow
		Plug 'luochen1990/rainbow'

		"color schem
		Plug 'joshdick/onedark.vim'
    Plug 'octol/vim-cpp-enhanced-highlight'

		"Modify tab line
		Plug 'mg979/vim-xtabline'

    "indent line

    "for version contral
    if has('nvim') || has('patch-8.0.902')
      Plug 'mhinz/vim-signify'
    else
      Plug 'mhinz/vim-signify', { 'branch': 'legacy' }
    endif

	"============== Functions ===============
	"========================================

		"TCommonts
		"<LEADER>__	normal one linel
		"g>c	g<c to uncomment
		"<LEADER>_r	one line after cursor
		"<LEADER>_b	one bloc
		"g>b	g<b to uncomment
		Plug 'tomtom/tcomment_vim'

		"surround the words
		"cs+-	chang + to -
		"ds remove the fances
		Plug 'tpope/vim-surround'

    "wilefire
    Plug 'gcmt/wildfire.vim'

    "多光标
    Plug 'mg979/vim-visual-multi'

    "Generate comments
    Plug 'babaybus/DoxygenToolkit.vim'

		"tabulize things
		Plug 'godlygeek/tabular'

		"make move lines so easy
		Plug 'matze/vim-move'

		"enhnace the f
		Plug 'rhysd/clever-f.vim'

    "Float terminal
    Plug 'voldikss/vim-floaterm'
		" Find & Replace
		"Plug 'brooth/far.vim', { 'on': ['F', 'Far', 'Fardo'] }

		"indentLine
		Plug 'Yggdroot/indentLine'

call plug#end()

"=============== 插件自定义选项 ==================
"=================================================

	colorscheme onedark


  " default updatetime 4000ms is not good for async update
  set updatetime=100

  "For TagBar
  "May need more configurations
  "nmap <LEADER>2 :TagbarToggle<CR>

  " For ranger
  tnoremap <silent> <M-i> <C-\><C-n>:RnvimrResize<CR>
  tnoremap <silent> <M-o> <C-\><C-n>:RnvimrToggle<CR>
  " Make Ranger to be hidden after picking a file
  let g:rnvimr_enable_picker = 1
  " Change the border's color
  let g:rnvimr_border_attr = {'fg': 14, 'bg': -1}
  " Draw border with both
  let g:rnvimr_ranger_cmd = 'ranger --cmd="set draw_borders both"'
  " Map Rnvimr action
  let g:rnvimr_action = {
              \ '<C-t>': 'NvimEdit tabedit',
              \ '<C-x>': 'NvimEdit split',
              \ '<C-v>': 'NvimEdit vsplit',
              \ 'gw': 'JumpNvimCwd',
              \ 'yw':'EmitRangerCwd'
              \}
  hi CocCursorRange guibg=#b16286 guifg=#ebdbb2

  "For airline
  source $HOME/.config/nvim/Bundles_conf/Airline.vim
  "For coc
  source $HOME/.config/nvim/Bundles_conf/Coc.vim
  "For defx
  "source /home/bruce/.config/nvim/Bundles_conf/Defx.vim
  "For gdb
  source $HOME/.config/nvim/Bundles_conf/Gdb.vim
  "For ranbow
  source $HOME/.config/nvim/Bundles_conf/Ranbow.vim
  "For tabline
  source $HOME/.config/nvim/Bundles_conf/Tabline.vim
 " For vista
  source $HOME/.config/nvim/Bundles_conf/Vista.vim
  "For whicn_key
  source $HOME/.config/nvim/Bundles_conf/Which_key.vim
  "For Floaterm 
  source $HOME/.config/nvim/Bundles_conf/Floaterm.vim
  "For markdown table mode
  source $HOME/.config/nvim/Bundles_conf/Vim-table-mode.vim
  "For markdown-preview
  source $HOME/.config/nvim/Bundles_conf/Markdown_preview.vim
